package com.fengye.security.security.service;

import com.fengye.security.domain.Role;
import com.fengye.security.security.repository.RoleRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.fengye.security.utils.Constants.ROLE_ADMIN;
import static com.fengye.security.utils.Constants.ROLE_STAFF;

/**
 * @author fengyexjtu@126.com
 * @date 2022年05月27日 5:02 PM
 */

@Service
@RequiredArgsConstructor
public class RoleHierarchyService {

    private final RoleRepository roleRepository;

    /**
     * 获取角色继承的表达式
     *
     * @return java.lang.String
     * @author fengyexjtu@126.com
     * @date 2022/5/27 5:03 PM
     */
    public String getRoleHierarchyExpr() {
        List<Role> all = roleRepository.findAll();
        return all.stream()
                .flatMap(role -> role.getPermissions().stream().map(permission -> role.getRoleName() + " > "
                        + permission.getAuthority() + " "))
                .collect(Collectors.joining(" ", ROLE_ADMIN + " > " + ROLE_STAFF + " ", ""));
    }
}
